package com.blockmeta.bbs.immodule.fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blockmeta.bbs.businesslibrary.base.fragment.BaseFragment;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.baselibrary.utils.OpenFileUtil;
import com.blockmeta.bbs.businesslibrary.widget.emoji.OnMediaClickListener;
import com.blockmeta.bbs.immodule.Constants;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.R2;
import com.blockmeta.bbs.immodule.activity.AddressBookActivity;
import com.blockmeta.bbs.immodule.activity.MyForumPostSendActivity;
import com.blockmeta.bbs.immodule.activity.MyLibraryBookSendActivity;
import com.blockmeta.bbs.immodule.dao.ChatMessage;
import com.blockmeta.bbs.immodule.widget.chatboard.BaseChatBoard;
import com.blockmeta.bbs.immodule.widget.chatboard.KeyboardEvent;
import com.hellobike.flutter.thrio.navigator.ThrioNavigator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.OnClick;
import me.rosuh.filepicker.config.FilePickerManager;

/**
 * A simple {@link Fragment} subclass.
 *
 * @author airthor
 * @date 2019/4/2
 * 输入框中发送图片等功能区
 */
public class MediaFragment extends BaseFragment{

    private OnMediaClickListener mOnMediaClickListener;
    protected BaseChatBoard.OnOutPutListener mOnOutPutListener;


    public void setOnMediaClickListener(OnMediaClickListener onMediaClickListener) {
        mOnMediaClickListener = onMediaClickListener;
    }

    public MediaFragment() {
        // Required empty public constructor
    }

    @Override
    public View setContentView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_media, container, false);
    }

    @Override
    public void initView() {
        List<Fragment> fragments = getFragmentManager().getFragments();
        for (Fragment fragment : fragments) {
            if (fragment instanceof BaseChatBoard){
                try {
                    mOnOutPutListener = ((BaseChatBoard) fragment).getOnOutPutListener();
                    setOnMediaClickListener((OnMediaClickListener) fragment);
                } catch (ClassCastException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }

    @OnClick({R2.id.iv_im_post, R2.id.iv_im_card, R2.id.iv_im_file, R2.id.iv_im_librarybook})
    public void onViewClicked(final View view) {
        int id = view.getId();
        if (id == R.id.iv_im_post){
            //活动
            mOnOutPutListener.onChatBoardEvent(KeyboardEvent.ACTIVITY);

        }else if (id == R.id.iv_im_card){
            //名片
            getActivity().startActivityForResult(new Intent(mContext, AddressBookActivity.class).putExtra(Constants.CANCHOOSE, true)
                    , Constant.CUSTOM_MESSAGE_CONTENT);

        }else if (id == R.id.iv_im_file){
            //文件
            FilePickerManager.INSTANCE
                    .from(this)
                    .maxSelectable(1)
                    .showCheckBox(false)
                    .forResult(FilePickerManager.REQUEST_CODE);
        }else if(id == R.id.iv_im_librarybook){
            getActivity().startActivityForResult(new Intent(mContext, MyLibraryBookSendActivity.class), Constant.REMOTE_File_MESSAGE_CONTENT);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("author", "chatboard result" + requestCode + " " + resultCode);
        //处理控件内部产生的内容，文字表情图片文件等结果回调到聊天页面进行发
        if(requestCode == FilePickerManager.REQUEST_CODE){
            if(resultCode == Activity.RESULT_OK){
                List<String> filePaths = FilePickerManager.INSTANCE.obtainData();
                if(filePaths.size() > 0){
                    if(mOnOutPutListener != null){
                        int chatMessageFileType = ChatMessage.getChatMessageFileType(OpenFileUtil.getExtensionName(filePaths.get(0)));
                        mOnOutPutListener.onOutPutFile(chatMessageFileType, filePaths.get(0));
                    }

                }

                Log.d("author", filePaths.toString());
            }else {
//                ToastUtil.show("没有选择文件");
            }
        }
    }
}
